import { PageParam } from '@/services/common'
import { covertTableData } from '@/utils/tableUtil'
import { request } from '@umijs/max'

export interface CustomerVO {
  /**
   * 客户id
   */
  id: string
  /**
   * 客户名
   */
  name?: string
  /**
   * 微信号
   */
  wxId?: string
  /**
   * 手机号
   */
  phone?: string
  /**
   * 拍摄类型
   */
  photoType: string
  /**
   * 渠道名
   */
  channelName?: string

  /**
   * 渠道标签颜色
   */
  channelTagColor: string
  /**
   * 跟进状态
   */
  followUpStateId: string
  attachUrl?: string
  /**
   * 创建时间
   */
  createTime: string
  /**
   * 备注
   */
  remark?: string

  sellerUserId?: string
  sellerUserName?: string
  creatorName: string
  brandId?: string
  brandName?: string
}

export interface CustomerQueryDTO extends PageParam {
  /**
   * 客户id
   */
  id?: string
  /**
   * 客户名
   */
  name?: string
  /**
   * 微信号
   */
  wxId?: string
  /**
   * 手机号
   */
  phone?: string
  /**
   * 拍摄类型
   */
  photoType?: string
  brandId?: string
  /**
   * 渠道id
   */
  channelId?: string
  /**
   * 跟进状态
   */
  followUpStateId?: string
}

export interface CustomerUpdateDTO {
  /**
   * 客户id
   */
  id?: string
  /**
   * 客户名
   */
  name?: string
  attachUrl?: string

  /**
   * 微信号
   */
  wxId?: string
  /**
   * 手机号
   */
  phone?: string
  /**
   * 拍摄类型
   */
  photoType?: string
  brandId?: string
  /**
   * 渠道id
   */
  channelId?: string
  /**
   * 跟进状态
   */
  followUpStateId?: string
  /**
   * 备注
   */
  remark?: string
  createTime: string
}

export const getCustomerById = async (id: string) => {
  return request<CustomerVO>(`/api/customer?id=${id}`, { method: 'GET' })
}

export const getCustomerList = async (params: CustomerQueryDTO) => {
  return request<Array<CustomerVO>>(`/api/customer/list`, { method: 'GET', params })
}

export const validateCustomerExist = async (params: CustomerQueryDTO) => {
  return request<Array<CustomerVO>>(`/api/customer/validateExist`, { method: 'GET', params })
}

export const getCustomerPage = async (params: CustomerQueryDTO) => {
  let response = await request<Array<CustomerVO>>(`/api/customer/page`, { method: 'GET', params })
  return covertTableData(response)
}

export const updateCustomer = async (customerUpdateDTO: CustomerUpdateDTO) => {
  return request<CustomerVO>(`/api/customer/update`, { method: 'POST', data: customerUpdateDTO })
}

export const saveCustomer = async (customerUpdateDTO: CustomerUpdateDTO) => {
  return request<CustomerVO>(`/api/customer/save`, { method: 'POST', data: customerUpdateDTO })
}

export const removeCustomer = async (id: string) => {
  return request<boolean>(`/api/customer/remove?id=${id}`, { method: 'POST' })
}

export interface FollowOptionType {
  label: string
  value: string
  total: number
}

export const getFollowOptions = async (params: CustomerQueryDTO) => {
  let d = request<FollowOptionType[]>(`/api/customer/getFollowOptions`, {
    method: 'GET',
    params: { ...params, followUpStateId: undefined },
  })
  return d
}
